package com.core.app.window.view;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.hezamu.googlemapwidget.GoogleMap;
import org.vaadin.hezamu.googlemapwidget.overlay.BasicMarker;

import com.core.app.CoreApplication;
import com.core.context.Inject;
import com.core.entity.Kecamatan;
import com.core.entity.Sekolah;
import com.core.service.KecamatanService;
import com.core.service.SekolahService;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;

// wkwk
public class HomeView extends CustomComponent {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private VerticalLayout verticalLayout_3;
	@AutoGenerated
	private Panel panel_2;
	@AutoGenerated
	private VerticalLayout verticalLayout_9;
	@AutoGenerated
	private Table hasilPencarianTable;
	@AutoGenerated
	private Panel panel_1;
	@AutoGenerated
	private VerticalLayout verticalLayout_4;
	@AutoGenerated
	private HorizontalSplitPanel googleMapLayout;
	@AutoGenerated
	private Accordion accordion_1;
	@AutoGenerated
	private VerticalLayout verticalLayout_5;
	@AutoGenerated
	private CheckBox akreditasiNoneCheckBox;
	@AutoGenerated
	private CheckBox akreditasiCCheckBox;
	@AutoGenerated
	private CheckBox akreditasiBCheckBox;
	@AutoGenerated
	private CheckBox akreditasiACheckBox;
	@AutoGenerated
	private VerticalLayout verticalLayout_6;
	@AutoGenerated
	private CheckBox smaCheckBox;
	@AutoGenerated
	private CheckBox smpCheckBox;
	@AutoGenerated
	private CheckBox sdCheckBox;
	@AutoGenerated
	private CheckBox tkCheckBox;
	@AutoGenerated
	private VerticalLayout verticalLayout_7;
	@AutoGenerated
	private Button searchButton;
	@AutoGenerated
	private TextField searchTextfield;
	@AutoGenerated
	private VerticalLayout viewMapLayout;
	@AutoGenerated
	private Label titleLabel;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private Button loginButton;
	private static final long serialVersionUID = 5457349652667574932L;
	private CoreApplication coreApplication;
	@Autowired
	private SekolahService sekolahService;
	@Autowired
	private KecamatanService kecamatanService;
	private GoogleMap googleMap;
	private VerticalLayout kecamatanAccordionItem;
	private ValueChangeListener checkedListener = new ValueChangeListener() {

		/**
		 * 
		 */
		private static final long serialVersionUID = -9206575073416270542L;

		@Override
		public void valueChange(ValueChangeEvent event) {
			filterChanged();
		}
	};

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 */
	public HomeView(final CoreApplication coreApplication) {
		Inject.Inject(this);
		setCoreApplication(coreApplication);
		buildMainLayout();
		buildMapLayout();
		buildKecamatanAccordion();
		buildSearchTab();
		buildTablePencarian();
		initEventFilter();
		titleLabel.addStyleName(Runo.LABEL_H1);
		setCompositionRoot(mainLayout);

		googleMapLayout.addStyleName(Runo.SPLITPANEL_SMALL);
		googleMapLayout.setSplitPosition(200, UNITS_PIXELS, true);
		googleMapLayout.setLocked(true);
		loginButton.addStyleName(Runo.BUTTON_SMALL);
		loginButton.addStyleName(Runo.BUTTON_DEFAULT);
		loginButton.addListener(new ClickListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 7352557378376948319L;

			@Override
			public void buttonClick(ClickEvent event) {
				coreApplication.showLoginView();
			}
		});

	}

	private void buildTablePencarian() {
		hasilPencarianTable.setSelectable(true);
		hasilPencarianTable.setMultiSelect(false);
		hasilPencarianTable.addStyleName(Runo.TABLE_BORDERLESS);
		hasilPencarianTable.addStyleName(Runo.TABLE_SMALL);

		hasilPencarianTable.addContainerProperty("Nomor", Integer.class, "");
		hasilPencarianTable.addContainerProperty("Nama", String.class, "");
		hasilPencarianTable.addContainerProperty("Detail", CssLayout.class, null);
		hasilPencarianTable.setColumnWidth("Nomor", 50);
		hasilPencarianTable.setVisibleColumns(new String[] {"Nomor", "Nama", "Detail" });
	}

	private void buildSearchTab() {
		searchButton.addListener(new ClickListener() {

			private static final long serialVersionUID = -177971634845848419L;

			@Override
			public void buttonClick(ClickEvent event) {
				filterChanged();

			}
		});

	}

	private void buildKecamatanAccordion() {
		kecamatanAccordionItem = new VerticalLayout();
		kecamatanAccordionItem.setImmediate(false);
		kecamatanAccordionItem.setWidth("100.0%");
		kecamatanAccordionItem.setHeight("-1px");
		kecamatanAccordionItem.setMargin(true);
		kecamatanAccordionItem.setSpacing(true);

		try {
			List<Kecamatan> kecamatans = kecamatanService.findAll();
			for (Kecamatan kec : kecamatans) {
				CheckBox kecCheckBox = new CheckBox();
				kecCheckBox.setCaption(kec.getNama());
				kecCheckBox.setImmediate(true);
				kecCheckBox.addListener(checkedListener);
				kecCheckBox.setData(kec);
				kecamatanAccordionItem.addComponent(kecCheckBox);
			}
		} catch (Exception e) {

			e.printStackTrace();
		}

		accordion_1.addTab(kecamatanAccordionItem, "Kecamatan");
	}

	private void initEventFilter() {

		tkCheckBox.setImmediate(true);
		tkCheckBox.addListener(checkedListener);
		sdCheckBox.setImmediate(true);
		sdCheckBox.addListener(checkedListener);
		smpCheckBox.setImmediate(true);
		smpCheckBox.addListener(checkedListener);
		smaCheckBox.setImmediate(true);
		smaCheckBox.addListener(checkedListener);
		akreditasiACheckBox.setImmediate(true);
		akreditasiACheckBox.addListener(checkedListener);
		akreditasiBCheckBox.setImmediate(true);
		akreditasiBCheckBox.addListener(checkedListener);
		akreditasiCCheckBox.setImmediate(true);
		akreditasiCCheckBox.addListener(checkedListener);
		akreditasiNoneCheckBox.setImmediate(true);
		akreditasiNoneCheckBox.addListener(checkedListener);
	}

	private void filterChanged() {
		buildMapLayout();

		String searchText = (String) searchTextfield.getValue();
		List<Object> namaSekolahFilter = new ArrayList<Object>();
		namaSekolahFilter.add(searchText);

		List<Object> tingkatanFilter = new ArrayList<Object>();
		if (tkCheckBox.booleanValue())
			tingkatanFilter.add("TK");
		if (sdCheckBox.booleanValue())
			tingkatanFilter.add("SD");
		if (smpCheckBox.booleanValue())
			tingkatanFilter.add("SMP");
		if (smaCheckBox.booleanValue())
			tingkatanFilter.add("SMA");

		List<Object> akreditasiFilter = new ArrayList<Object>();
		if (akreditasiACheckBox.booleanValue())
			akreditasiFilter.add('A');
		if (akreditasiBCheckBox.booleanValue())
			akreditasiFilter.add('B');
		if (akreditasiCCheckBox.booleanValue())
			akreditasiFilter.add('C');
		if (akreditasiNoneCheckBox.booleanValue())
			akreditasiFilter.add('-');

		List<Object> kecamatanFilter = new ArrayList<Object>();
		for (int i = 0; i < kecamatanAccordionItem.getComponentCount(); i++) {
			CheckBox kecCheckBox = (CheckBox) kecamatanAccordionItem
					.getComponent(i);
			if (kecCheckBox.booleanValue()) {
				kecamatanFilter.add((Kecamatan) kecCheckBox.getData());
			}
		}

		Map<String, List<Object>> param = new HashMap<String, List<Object>>();
		param.put("nama", namaSekolahFilter);
		param.put("tingkatan", tingkatanFilter);
		param.put("akreditasi", akreditasiFilter);
		param.put("kecamatan", kecamatanFilter);

		List<Sekolah> sekolahs = sekolahService.search(param);

		googleMap.getOverlays().clear();
		for (Sekolah s : sekolahs) {
			BasicMarker basicMarker = new BasicMarker(s.getId(), s.getPoint(),
					s.getNama());
			basicMarker
					.setIconUrl("http://google-maps-icons.googlecode.com/files/school.png");
			basicMarker.setDraggable(false);
			HorizontalLayout info = new HorizontalLayout();
			info.setWidth("200px");
			info.addComponent(new Label(s.getNama()));
			basicMarker.setInfoWindowContent(HomeView.this, info);
			googleMap.addMarker(basicMarker);

		}

		if (hasilPencarianTable.removeAllItems()) {
			int c = 1;
			for (Sekolah sekolah : sekolahs) {
				Item item = hasilPencarianTable.addItem(sekolah.getId());
				Button detailButton = new Button("Detail");
				detailButton.setStyleName(Runo.BUTTON_SMALL);
				detailButton.setEnabled(true);
				detailButton.setImmediate(true);
				CssLayout detailButtonLayout = new CssLayout();
				detailButtonLayout.addComponent(detailButton);
				
				item.getItemProperty("Nomor").setValue(new Integer(c));
				item.getItemProperty("Nama").setValue(sekolah.getNama());
				item.getItemProperty("Detail").setValue(detailButtonLayout);
//				hasilPencarianTable.addItem(new Object[]{c, sekolah.getNama()}, sekolah.getId());
				c++;
			}
		}
	}

	private void buildMapLayout() {
		viewMapLayout.removeAllComponents();
		googleMap = new GoogleMap(coreApplication, new Point2D.Double(
				110.394745, -7.016394), 12);
		googleMap.setWidth("100%");
		googleMap.setHeight("100%");
		googleMap
				.addControl(org.vaadin.hezamu.googlemapwidget.GoogleMap.MapControl.LargeMapControl);
		googleMap
				.addControl(org.vaadin.hezamu.googlemapwidget.GoogleMap.MapControl.ScaleControl);
		viewMapLayout.addComponent(googleMap);
	}

	public CoreApplication getCoreApplication() {
		return coreApplication;
	}

	public void setCoreApplication(CoreApplication coreApplication) {
		this.coreApplication = coreApplication;
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(false);
		mainLayout.setSpacing(true);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("-1px");
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		mainLayout.addComponent(verticalLayout_2);
		mainLayout.setComponentAlignment(verticalLayout_2, new Alignment(48));
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("900px");
		verticalLayout_2.setHeight("-1px");
		verticalLayout_2.setMargin(false);
		
		// verticalLayout_3
		verticalLayout_3 = buildVerticalLayout_3();
		verticalLayout_2.addComponent(verticalLayout_3);
		verticalLayout_2.setExpandRatio(verticalLayout_3, 1.0f);
		
		return verticalLayout_2;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_3() {
		// common part: create layout
		verticalLayout_3 = new VerticalLayout();
		verticalLayout_3.setImmediate(false);
		verticalLayout_3.setWidth("100.0%");
		verticalLayout_3.setHeight("100.0%");
		verticalLayout_3.setMargin(true);
		verticalLayout_3.setSpacing(true);
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		verticalLayout_3.addComponent(horizontalLayout_1);
		
		// titleLabel
		titleLabel = new Label();
		titleLabel.setImmediate(false);
		titleLabel.setWidth("-1px");
		titleLabel.setHeight("-1px");
		titleLabel.setValue("Sistem Informasi Penyebaran Sekolah di Semarang");
		verticalLayout_3.addComponent(titleLabel);
		verticalLayout_3.setComponentAlignment(titleLabel, new Alignment(20));
		
		// panel_1
		panel_1 = buildPanel_1();
		verticalLayout_3.addComponent(panel_1);
		
		// panel_2
		panel_2 = buildPanel_2();
		verticalLayout_3.addComponent(panel_2);
		
		return verticalLayout_3;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("100.0%");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		horizontalLayout_1.setSpacing(true);
		
		// loginButton
		loginButton = new Button();
		loginButton.setCaption("Login");
		loginButton.setImmediate(true);
		loginButton.setWidth("100px");
		loginButton.setHeight("-1px");
		horizontalLayout_1.addComponent(loginButton);
		horizontalLayout_1
				.setComponentAlignment(loginButton, new Alignment(34));
		
		return horizontalLayout_1;
	}

	@AutoGenerated
	private Panel buildPanel_1() {
		// common part: create layout
		panel_1 = new Panel();
		panel_1.setCaption("Peta Semarang");
		panel_1.setImmediate(false);
		panel_1.setWidth("100.0%");
		panel_1.setHeight("500px");
		
		// verticalLayout_4
		verticalLayout_4 = buildVerticalLayout_4();
		panel_1.setContent(verticalLayout_4);
		
		return panel_1;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_4() {
		// common part: create layout
		verticalLayout_4 = new VerticalLayout();
		verticalLayout_4.setImmediate(false);
		verticalLayout_4.setWidth("100.0%");
		verticalLayout_4.setHeight("100.0%");
		verticalLayout_4.setMargin(false);
		
		// googleMapLayout
		googleMapLayout = buildGoogleMapLayout();
		verticalLayout_4.addComponent(googleMapLayout);
		
		return verticalLayout_4;
	}

	@AutoGenerated
	private HorizontalSplitPanel buildGoogleMapLayout() {
		// common part: create layout
		googleMapLayout = new HorizontalSplitPanel();
		googleMapLayout.setImmediate(false);
		googleMapLayout.setWidth("100.0%");
		googleMapLayout.setHeight("100.0%");
		googleMapLayout.setMargin(false);
		
		// viewMapLayout
		viewMapLayout = new VerticalLayout();
		viewMapLayout.setImmediate(false);
		viewMapLayout.setWidth("100.0%");
		viewMapLayout.setHeight("100.0%");
		viewMapLayout.setMargin(false);
		googleMapLayout.addComponent(viewMapLayout);
		
		// accordion_1
		accordion_1 = buildAccordion_1();
		googleMapLayout.addComponent(accordion_1);
		
		return googleMapLayout;
	}

	@AutoGenerated
	private Accordion buildAccordion_1() {
		// common part: create layout
		accordion_1 = new Accordion();
		accordion_1.setImmediate(true);
		accordion_1.setWidth("100.0%");
		accordion_1.setHeight("100.0%");
		
		// verticalLayout_7
		verticalLayout_7 = buildVerticalLayout_7();
		accordion_1.addTab(verticalLayout_7, "Pencarian", null);
		
		// verticalLayout_6
		verticalLayout_6 = buildVerticalLayout_6();
		accordion_1.addTab(verticalLayout_6, "Tingkatan Sekolah", null);
		
		// verticalLayout_5
		verticalLayout_5 = buildVerticalLayout_5();
		accordion_1.addTab(verticalLayout_5, "Akreditasi", null);
		
		return accordion_1;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_7() {
		// common part: create layout
		verticalLayout_7 = new VerticalLayout();
		verticalLayout_7.setImmediate(false);
		verticalLayout_7.setWidth("100.0%");
		verticalLayout_7.setHeight("-1px");
		verticalLayout_7.setMargin(true);
		verticalLayout_7.setSpacing(true);
		
		// searchTextfield
		searchTextfield = new TextField();
		searchTextfield.setCaption("Nama Sekolah");
		searchTextfield.setImmediate(false);
		searchTextfield.setWidth("100.0%");
		searchTextfield.setHeight("-1px");
		verticalLayout_7.addComponent(searchTextfield);
		
		// searchButton
		searchButton = new Button();
		searchButton.setCaption("Cari");
		searchButton.setImmediate(true);
		searchButton.setWidth("100px");
		searchButton.setHeight("-1px");
		verticalLayout_7.addComponent(searchButton);
		verticalLayout_7.setComponentAlignment(searchButton, new Alignment(48));
		
		return verticalLayout_7;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_6() {
		// common part: create layout
		verticalLayout_6 = new VerticalLayout();
		verticalLayout_6.setImmediate(false);
		verticalLayout_6.setWidth("100.0%");
		verticalLayout_6.setHeight("-1px");
		verticalLayout_6.setMargin(true);
		verticalLayout_6.setSpacing(true);
		
		// tkCheckBox
		tkCheckBox = new CheckBox();
		tkCheckBox.setCaption("TK/Play Group");
		tkCheckBox.setImmediate(false);
		tkCheckBox.setWidth("-1px");
		tkCheckBox.setHeight("-1px");
		verticalLayout_6.addComponent(tkCheckBox);
		
		// sdCheckBox
		sdCheckBox = new CheckBox();
		sdCheckBox.setCaption("Sekolah Dasar");
		sdCheckBox.setImmediate(false);
		sdCheckBox.setWidth("-1px");
		sdCheckBox.setHeight("-1px");
		verticalLayout_6.addComponent(sdCheckBox);
		
		// smpCheckBox
		smpCheckBox = new CheckBox();
		smpCheckBox.setCaption("Sekolah Menengah Pertama");
		smpCheckBox.setImmediate(false);
		smpCheckBox.setWidth("-1px");
		smpCheckBox.setHeight("-1px");
		verticalLayout_6.addComponent(smpCheckBox);
		
		// smaCheckBox
		smaCheckBox = new CheckBox();
		smaCheckBox.setCaption("Sekolah Menengah Atas");
		smaCheckBox.setImmediate(false);
		smaCheckBox.setWidth("-1px");
		smaCheckBox.setHeight("-1px");
		verticalLayout_6.addComponent(smaCheckBox);
		
		return verticalLayout_6;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_5() {
		// common part: create layout
		verticalLayout_5 = new VerticalLayout();
		verticalLayout_5.setImmediate(false);
		verticalLayout_5.setWidth("100.0%");
		verticalLayout_5.setHeight("-1px");
		verticalLayout_5.setMargin(true);
		verticalLayout_5.setSpacing(true);
		
		// akreditasiACheckBox
		akreditasiACheckBox = new CheckBox();
		akreditasiACheckBox.setCaption("A");
		akreditasiACheckBox.setImmediate(false);
		akreditasiACheckBox.setWidth("-1px");
		akreditasiACheckBox.setHeight("-1px");
		verticalLayout_5.addComponent(akreditasiACheckBox);
		
		// akreditasiBCheckBox
		akreditasiBCheckBox = new CheckBox();
		akreditasiBCheckBox.setCaption("B");
		akreditasiBCheckBox.setImmediate(false);
		akreditasiBCheckBox.setWidth("-1px");
		akreditasiBCheckBox.setHeight("-1px");
		verticalLayout_5.addComponent(akreditasiBCheckBox);
		
		// akreditasiCCheckBox
		akreditasiCCheckBox = new CheckBox();
		akreditasiCCheckBox.setCaption("C");
		akreditasiCCheckBox.setImmediate(false);
		akreditasiCCheckBox.setWidth("-1px");
		akreditasiCCheckBox.setHeight("-1px");
		verticalLayout_5.addComponent(akreditasiCCheckBox);
		
		// akreditasiNoneCheckBox
		akreditasiNoneCheckBox = new CheckBox();
		akreditasiNoneCheckBox.setCaption("-");
		akreditasiNoneCheckBox.setImmediate(false);
		akreditasiNoneCheckBox.setWidth("-1px");
		akreditasiNoneCheckBox.setHeight("-1px");
		verticalLayout_5.addComponent(akreditasiNoneCheckBox);
		
		return verticalLayout_5;
	}

	@AutoGenerated
	private Panel buildPanel_2() {
		// common part: create layout
		panel_2 = new Panel();
		panel_2.setCaption("Hasil Pencarian");
		panel_2.setImmediate(false);
		panel_2.setWidth("100.0%");
		panel_2.setHeight("250px");
		
		// verticalLayout_9
		verticalLayout_9 = buildVerticalLayout_9();
		panel_2.setContent(verticalLayout_9);
		
		return panel_2;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_9() {
		// common part: create layout
		verticalLayout_9 = new VerticalLayout();
		verticalLayout_9.setImmediate(false);
		verticalLayout_9.setWidth("100.0%");
		verticalLayout_9.setHeight("-1px");
		verticalLayout_9.setMargin(false);
		
		// hasilPencarianTable
		hasilPencarianTable = new Table();
		hasilPencarianTable.setImmediate(false);
		hasilPencarianTable.setWidth("100.0%");
		hasilPencarianTable.setHeight("300px");
		verticalLayout_9.addComponent(hasilPencarianTable);
		
		return verticalLayout_9;
	}

}
